package Game;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
/** 
 * @author xuaner
 * @creed: Talk is cheap,show me the code
 * @date 2021/9/11 13:42
 */


public class MainFrame extends Frame {

    Thread t1 = new Thread();
    Thread t2 = new Thread();

    Image player1Img = getImage("images/p1.gif");
    Image player2Img = getImage("images/p2.gif");
    Image background = getImage("images/bgcolor.jpg");
    Image lightImg = getImage("images/light.png");
    Image unlightImg = getImage("images/unlight.png");



    Player player1 = new Player(player1Img,250,250);
    Player player2 = new Player(player2Img,350,350);

    Date startTime = new Date(); //开始时间
    Date endTime;
    int period; //游戏持续时间
    int ti = 0;  //用于电灯 每一秒钟刷新的次数 一秒刷新21次
    int ti2 = 0;  //用于buff 每一秒钟刷新的次数 一秒刷新21次
    int lf;   //随机生成数字 表示哪盏灯亮

    int roundBuff;  //随机生成buff类型 奇数表示2倍速 偶数表示2倍分

    boolean bf=false;
    int point1=0;
    int point2=0;

    int buffx,buffy;

    public void drawLight(Graphics g,int Lightflag){
        if(Lightflag==0) {
            g.drawImage(lightImg,450,50,null);//上灯
            g.drawImage(unlightImg,450,585,null);//下灯
            g.drawImage(unlightImg,0,295,null);//左灯
            g.drawImage(unlightImg,920,295,null);//右灯
        }
        if(Lightflag==1) {
            g.drawImage(lightImg,450,585,null);//下灯
            g.drawImage(unlightImg,0,295,null);//左灯
            g.drawImage(unlightImg,920,295,null);//右灯
            g.drawImage(unlightImg,450,50,null);//上灯
        }
        if(Lightflag==2) {
            g.drawImage(lightImg,0,295,null);//左灯
            g.drawImage(unlightImg,450,50,null);//上灯
            g.drawImage(unlightImg,450,585,null);//下灯
            g.drawImage(unlightImg,920,295,null);//右灯
        }
        if(Lightflag==3) {
            g.drawImage(lightImg,920,295,null);//右灯
            g.drawImage(unlightImg,450,50,null);//上灯
            g.drawImage(unlightImg,450,585,null);//下灯
            g.drawImage(unlightImg,0,295,null);//左灯
        }
    }

    public void drawShadow(Graphics g,int Lightflag){
        if(Lightflag==0) {
            g.fillRect((int)player1.getX(),(int)player1.getY()+60,50,80);
            g.fillRect((int)player2.getX(),(int)player2.getY()+60,50,80);
            check(player1,player2,0,60,50,80);
        }
        if(Lightflag==1) {
            g.fillRect((int)player1.getX(),(int)player1.getY()-80,50,80);
            g.fillRect((int)player2.getX(),(int)player2.getY()-80,50,80);
            check(player1,player2,0,-80,50,80);
        }
        if(Lightflag==2) {
            g.fillRect((int)player1.getX()+60,(int)player1.getY(),80,50);
            g.fillRect((int)player2.getX()+60,(int)player2.getY(),80,50);
            check(player1,player2,60,0,80,50);
        }
        if(Lightflag==3) {
            g.fillRect((int)player1.getX()-80,(int)player1.getY(),80,50);
            g.fillRect((int)player2.getX()-80,(int)player2.getY(),80,50);
            check(player1,player2,-80,0,80,50);
        }
    }

    public void drawBuff(Graphics g,Buff buff,boolean bf){
            buff.BuffFlag = bf;
            buff.AddBuff(g);
    }

    public void check(Player player1,Player player2,int x,int y,int width,int height){
        //碰撞检测
        boolean peng1 = player1.getRect().intersects((int)player2.getX()+x,(int)player2.getY()+y,width,height);
        if(peng1){
            System.out.println("1." + peng1);
            point1+=player1.score;
        }

        boolean peng2 = player2.getRect().intersects((int)player1.getX()+x,(int)player1.getY()+y,width,height);

        if(peng2){
            System.out.println("2."+peng2);
            point2+=player2.score;
        }

    }

    public void paint(Graphics g){   //自动被调用

        g.drawImage(background, 0, 0, null);

        //计分板
        Color c = g.getColor();

        g.drawImage(background, 0, 0, null);
        g.setColor(Color.white);//设置面板颜色位白色
        g.fillRect(0,0,150,130);
        g.setColor(c);//恢复原有的色彩颜色

        //分数面板
        g.setFont(new Font("宋体",Font.BOLD,18));
        g.drawString("玩家1：" + point1,40,60);
        g.drawString("玩家2：" + point2,40,85);
        g.setFont(new Font("宋体",Font.LAYOUT_LEFT_TO_RIGHT,15));
        g.drawString("游戏说明：红色小人代表玩家1(W/A/S/D操作) 蓝色小人代表玩家2(↑↓←→操作) 每20秒刷新红色2加速buff 绿色2倍得分buff 出发吧！" ,10,680);

        //计时
        endTime = new Date();
        period = (int) ((endTime.getTime() - startTime.getTime()) / 1000);  //计时功能
        g.setFont(new Font("宋体",Font.BOLD,18));
        g.drawString("时间：" + period,40,110);
//        System.out.println(period);
      if(period<=60) {
        if(period%15==0){
            ti++;      //ti代表每一秒钟刷新的次数 一秒刷新21次
//            System.out.println("ti" + ti);
            if(ti%21==0) {
                lf = new Random().nextInt(4);//生成随机数
                System.out.println("改变灯"+lf);
            }
        }

        if(period%20==0){
            ti2++;
            if(ti2%21==0) {
                buffx = new Random().nextInt(1000);
                buffy = new Random().nextInt(600);
                roundBuff = new Random().nextInt(100);
                bf=true;
            }

        }

        drawLight(g,lf);
        drawShadow(g,lf);
        Buff buff;
        int buffFlag;
        if(roundBuff%2!=0){
            buff = new BuffPool(buffx,buffy);
            drawBuff(g,buff,bf);
            buffFlag=0;
        }else {
            buff = new DoublePoints(buffx,buffy);
            drawBuff(g,buff,bf);
            buffFlag=1;
        }


        //碰撞检测
        boolean peng3 = player1.getRect().intersects(buff.getRect());

        if(peng3){
            bf=false;
            if(buffFlag==0){
                player1.setSpeed(20);
                System.out.println("1加速度buff");
            }else{
                player1.score = 2;
                System.out.println("1加分buff");

            }
        }

        boolean peng4 = player2.getRect().intersects(buff.getRect());

        if(peng4){
            bf=false;
            if(buffFlag==0){
                player2.setSpeed(20);
                System.out.println("2加速度buff");
            }else{
                player2.score = 2;
                System.out.println("2加分buff");

            }
        }

            player1.drawSelf(g);
            player2.drawSelf(g);
        }
      else {
          g.setColor(Color.blue);
          g.setFont(new Font("黑体", Font.BOLD, 30));
          g.drawString("玩家1得分:" + point1 + "分", 400,350);
          g.drawString("玩家2得分:" + point2 + "分", 400,400);
          if(point1>point2){
              g.drawString("玩家1胜利", 400,450);
          }else if(point1<point2){
              g.drawString("玩家2胜利", 400,450);
          }else{
              g.drawString("平局", 400,450);
          }
      }
    }

    //多线程反复重画窗口
    class PanintThread extends Thread{
        public void run(){
            while (true){
                repaint(); //重画
                try {
                    t1.sleep(40);//1s=1000ms   每40ms刷新一次 相当于1秒刷新21次
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
            }
        }

    }

    //键盘监听
    class KeyMonitor extends KeyAdapter{
        //		键盘按下
        @Override
        public void keyPressed(KeyEvent e) {
            // TODO 自动生成的方法存根
            System.out.println("按下:"+e.getKeyCode());
            if(e.getKeyCode()==65||e.getKeyCode()==68||e.getKeyCode()==83||e.getKeyCode()==87)
                player1.addDirection1(e);
            if (e.getKeyCode()==37||e.getKeyCode()==38||e.getKeyCode()==39||e.getKeyCode()==40)
                player2.addDirection2(e);
        }
        //		键盘抬起
        public void keyReleased(KeyEvent e) {
            // TODO 自动生成的方法存根
//			System.out.println("抬起:"+e.getKeyCode());
            if(e.getKeyCode()==65||e.getKeyCode()==68||e.getKeyCode()==83||e.getKeyCode()==87)
                player1.minusDirection1(e);
            if (e.getKeyCode()==37||e.getKeyCode()==38||e.getKeyCode()==39||e.getKeyCode()==40)
                player2.minusDirection2(e);
        }
    }



    //游戏窗口编写
    public void launchFrame(){
        this.setTitle("踩影子");
        this.setVisible(true); //显示窗口
        this.setSize(1000,695);
        this.setLocation(300,100);

        //使程序可以正常关闭窗口
        this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });

        new PanintThread().start();  //启动重画窗口线程
        addKeyListener(new KeyMonitor());  //增加键盘监听

    }

    public static Image getImage(String path) {

        BufferedImage pen =null;
        try {
            URL u = MainFrame.class.getClassLoader().getResource(path);   //反射
            pen = ImageIO.read(u);

        }catch(IOException e) {
            e.printStackTrace();
        }
        return pen;


    }

    private Image offScreenImage = null;
    //添加双缓冲来防止窗口闪烁
    public void update(Graphics g) {
        if(offScreenImage == null)
            offScreenImage = this.createImage(1000,700);

        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage,0,0,null);

    }

    public static void main(String[] args) {
        MainFrame mainFrame = new MainFrame();
        mainFrame.launchFrame();


    }


}



